package xcu.lxj.nettychat.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xcu.lxj.nettychat.common.domain.vo.request.IdReqVO;
import xcu.lxj.nettychat.common.domain.vo.response.ApiResult;
import xcu.lxj.nettychat.common.domain.vo.response.CursorPageBaseResp;
import xcu.lxj.nettychat.common.utils.RequestHolder;
import xcu.lxj.nettychat.user.domain.vo.request.FuzzyMatchCursorPageReq;
import xcu.lxj.nettychat.user.domain.vo.request.LoginReq;
import xcu.lxj.nettychat.user.domain.vo.response.LoginSuccessResp;
import xcu.lxj.nettychat.user.domain.vo.response.UserInfoResp;
import xcu.lxj.nettychat.user.service.inter.LoginService;
import xcu.lxj.nettychat.user.service.inter.UserService;

/**
 * @Author XinJian Lei
 * @Date 2024/9/28 15:49
 * @Version 1.0
 */
@Slf4j
@RestController
@Tag(name="用户接口")
public class UserController {


    @Autowired
    LoginService loginService;
    @Autowired
    UserService userService;

    @Data
    static class LoginTest{
        private String email;
    }


    @Operation(summary = "登录")
    @PostMapping("/login")
    public ApiResult<LoginSuccessResp> login(@Valid @RequestBody LoginReq loginReq){
        String token = loginService.login(loginReq.getEmail(), loginReq.getPassword());
//        log.info("userid = {} , token = {}",RequestHolder.get(),token);
        return ApiResult.success(LoginSuccessResp.builder().token(token).build());
    }

    @GetMapping("/userInfo")
    @Operation(summary = "用户详情")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }


    @PostMapping("/likeUsername")
    @Operation(summary = "搜索好友 目的 ：去添加好友")
    public ApiResult<CursorPageBaseResp<UserInfoResp>> getUserInfoByName(@Valid @RequestBody FuzzyMatchCursorPageReq request){

        return ApiResult.success(userService.getUserInfoByName(request));
    }

    @PostMapping("/userInfoByUid")
    @Operation(summary = "根据用户查询基本信息")
    public ApiResult<UserInfoResp> getUserInfoByUid(@RequestBody IdReqVO idReqVO){

        return ApiResult.success(userService.getUserInfo(idReqVO.getId()));
    }


}
